// https://www.acwing.com/problem/content/839/

#include <iostream>
#include <vector>

using namespace std;

const int MAX_ARRAY_SIZE = 1e5+5;

// 存储每个节点的父节点
int parents[MAX_ARRAY_SIZE];
int neighbors[MAX_ARRAY_SIZE];

int find(int x) {
    if (parents[x] != x) {
        parents[x] = find(parents[x]);
    }
    return parents[x];
}

int main() {
    int m, n, a, b;
    string word;
    cin>>n>>m;
    for (int i=1; i<=n; i++) {
        parents[i] = i;
        neighbors[i] = 1;
    }
    while (m-->0) {
        cin>>word;
        if (word == "C" ) {
            cin>>a>>b;
            int r1 = find(a);
            int r2 = find(b);
            if (r1 != r2) {
                neighbors[r2] += neighbors[r1];
                parents[r1] = r2;
            }
        } else if (word == "Q1") {
            cin>>a>>b;
            if (find(a) == find(b)) {
                cout<<"Yes"<<endl;
            } else {
                cout<<"No"<<endl;
            }
        } else if (word == "Q2") {
            cin>>a;
            cout<<neighbors[find(a)]<<endl;
        }
    }


    return 0;
}

